package com.yueqianshop.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.yueqianshop.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117692188";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCj/YPVKx2Yc1rf9cs0nqb99zIE89R8E48UfxsruVKipkJvnGLq506Fn6SP9/C5y6EPg+1/iTCEDw7g64HCBXVsjJVv6qCur0rXs6AnhR5kokVvw4UYMWAMUs/VccSTq83AcTQ08RI9bQ5FY3RmCxjTzDJGzUPMEtGjrxBM0WeYIsEzFWaag6ArEt1f8rd4ch91nBb1zxPrhbzlDrUUjguwe/nuNGZ3DerRR1r3rN7hbXlS7Mmze/slyzNxyua3slxWgf+dd/2dq3/JOik0tqXNSfJmqqbed63fUlcTwHwiN2GO9EpOgPkn2Sa04wD8akrAivxifFf4o0Lcx6xrxs1xAgMBAAECggEAeMvel2B6HXkjgIOa2rjfA4ZaJwHBDGCP7aYvSobpkvvrRjGpP6WA1rcgzhfyD7oqerZQdLiGr1A0QoPYAiZpN7tavKTVFbfz5m3fVB0fpTtO8KPSX17HhGxfwbda+Pf9yHra3QCS0gKKF5zJ+fzEngb1Jeg/v4xBNlRXKm6543rOcAts7P4uDDwQvjMxBG9AXLWIZ2E0Ek1l+ZMFNY4zS9Y/V0wk0daTifoIV3Ju/77sOXtrVgF2WZnSjKEJxxmM1LWqCH8/PibnOGnTTcquaDy8W9V5GUyXNTixciML5oy8QL4adz3Rpf68Fc3sG2NqSfZ4kZ7Kb4X36dlgNnKuLQKBgQDmSpbinM6cgw4ScEPcNpzrur5Q6TG4A/GGovyaGPlXVAnwN39rzC87C70sQIO9LnXEv+wTiO1aQnfSjIWTWqXqfDWps5btFix4U2gHvhrrISwJ4p/2Ug8A5WkNfIJkrI8QE6+R9+ocgMXkMrPBk3OHznVqO2ZW5saka4VpFlyqpwKBgQC2TCHQmb7yTYiqqT0tCpBkXRAdLAP6tZyvQXSiqTfzjmLCWrK656UYNH2LOm0DKeskkKve4Urpxte2flJMv/1F+uMzXS9f6DSS34eVVFynoppCYx82YVm+xMP/tgH2FcAPhrMgXUps/qKy7EZ/Dezk+QGBzEKo+FzxVtEZAwCCJwKBgQDHyJfDepPHte5EUr6cUYiq/HPUz6lBYbJrwgPwT5AokZCZpBvX0eh7igCQXluriJ3ElVdhzx1Se21l7GCJaE+d8CnB0jox7lWgAYOzi9ngeAOzg7aQQ7oeZedQOt8IJadrJBjhQjkIhzKmeNH3Mks82wCDq4pqA4T66QhxFWRI6wKBgDNlQX9baV9NBsP+1PWW7eCt8DYZS8HqZBNxI94cUV8EfMmI4q0ZVA1/9wlC3qsurVHwy9QZtpI0cmrMMI3c7x5EvrujgRDMGc5y4Pnz/+9vQ4iGA63EQpYrSYsVmHX16eE2u9fbFXmnTN2fCb0WgvyHS6p5mS2kYCfWrzsbApCvAoGAE+qPNZkxsWGD/QFAnQS91SYRF1gJa0oLX16mUSChrNoO9+kzUl85K4ZxjmoG5O+G8V/KSAjQgoFyi5QtteofcxGIosZSWO4ancmOQcIZN7Rqx9fMZy2tf+723gwWxA/aY+155dguG6Fk+HcLZBXri3G4x1keGCWa6WBc4h9EJ5E=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo+VGfP3O0hJ1uhNG/ZyeZ9RpI9PREAygNxQj02WVymfc4BRl+wlFffSPRSTOjaVOqBHgdMdFW8NP9+RsfqfHnm8Nbe2tGRwFmQHC1PKVyF1C5bejtDaOzyJ3NqlI5tZ6WODLUTqnyuiThtw4f0334n43Yw0eEdL56vjpXeUpPuuZYJ9a6nMrQWMy3EktpL0x/0NmC7MJt1oCTzMvprpMCvFNJMVJ36df5dh3zhNvakUaM7c5RXT3c20ZA1PjlFypEqoV4Q74kWk9vMwy0RgTA4DJ3U7zJe2wwZx2uIo/l27pcasJsZA9Z2xTfoiLAfWmARZEfcYTbn3N/8I7u3Wr/wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://localhost:9000/payed/notify";


    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://order.yueqianshop.com/orderList.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
